using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using HestiaBackup.Library.Data;

namespace HestiaBackup.Library.ConfigurationManager
{
    public class Settings : IDisposable
    {
        private Projects _projects;

        private string _fileName;

        /// <summary>
        /// Get the file name where configuration is saved
        /// </summary>
        public string FileName
        {
            get { return _fileName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Value cannot be null or empty");
                }
                _fileName = value;
            }
        }

        public Settings(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }
            _projects = new Projects();
            _fileName = fileName;
            RefreshData();
        }


        public void RefreshData()
        {
            if (File.Exists(FileName))
            {
                _projects.ReadXml(FileName);
            }
            ReadFtpSettings();
        }

        private void ReadFtpSettings()
        {
            _FtpSettings = new List<FtpSetting>();
            for (int i = 0; i < _projects.FtpProjets.Rows.Count; i++)
            {
                Projects.FtpProjetsRow row = (Projects.FtpProjetsRow) _projects.FtpProjets.Rows[i];
                FtpSetting _NewFtpSetting = new FtpSetting();
                _NewFtpSetting.ID = row.ID;
                _NewFtpSetting.FriendlyName = row.FriendlyName;
                _NewFtpSetting.KeepDeletedFiles = row.KeepDeletedFiles;
                _NewFtpSetting.LastRunEnd = row.LastRunEnd;
                _NewFtpSetting.LastRunStart = row.LastRunStart;
                _NewFtpSetting.LastRunSuccess = row.LastRunSuccess;
                _NewFtpSetting.LocalDeletedFolder = row.LocalDeletedFolder;
                _NewFtpSetting.LocalFolder = row.LocalFolder;
                _NewFtpSetting.LogFileName = row.LogFileName;
                _NewFtpSetting.ProcessDate = row.ProcessHour;
                _NewFtpSetting.RemoteFolder = row.RemoteFolder;
                _NewFtpSetting.Server = row.Server;
                _NewFtpSetting.User = row.User;
                _NewFtpSetting.EncryptedPassword = row.Pass;
                _NewFtpSetting.IsRunning = row.Running;
                _NewFtpSetting.Enabled = row.Enabled;
                _FtpSettings.Add(_NewFtpSetting);
            }
        }

        public void SaveData()
        {
            _projects = new Projects();
            for (int i = 0; i < FtpSettings.Count; i++)
            {
                Projects.FtpProjetsRow row = _projects.FtpProjets.NewFtpProjetsRow();
                row.FriendlyName = FtpSettings[i].FriendlyName;
                row.KeepDeletedFiles = FtpSettings[i].KeepDeletedFiles;
                row.LastRunEnd = FtpSettings[i].LastRunEnd;
                row.LastRunStart = FtpSettings[i].LastRunStart;
                row.LastRunSuccess = FtpSettings[i].LastRunSuccess;
                row.LocalDeletedFolder = FtpSettings[i].LocalDeletedFolder;
                row.LocalFolder = FtpSettings[i].LocalFolder;
                row.LogFileName = FtpSettings[i].LogFileName;
                row.Pass = FtpSettings[i].EncryptedPassword;
                row.ProcessHour = FtpSettings[i].ProcessDate;
                row.RemoteFolder = FtpSettings[i].RemoteFolder;
                row.Server = FtpSettings[i].Server;
                row.User = FtpSettings[i].User;
                row.Running = FtpSettings[i].IsRunning;
                row.Enabled = FtpSettings[i].Enabled;
                _projects.FtpProjets.Rows.Add(row);
            }
            _projects.WriteXml(FileName, XmlWriteMode.WriteSchema);
        }

        private List<FtpSetting> _FtpSettings;

        public List<FtpSetting> FtpSettings
        {
            get { return _FtpSettings; }
        }


        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _projects.Dispose();
            }
        }

        #endregion
    }
}